﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;
using bs_ShowRoom;


namespace ShowRoom.admin
{
    public partial class user : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user_id"] == null || Session["userId"] == "")
            {
                Response.Redirect("login.aspx");
            }
            if (!IsPostBack)
            {
                GetData();
            }
            if (txtDeleteUser.Value != "")
            {
                string Type_ = "6";
                SaveData(txtDeleteUser.Value, "", "", "", "", "", "", "", "", "", "", "", "", Type_);
                // fnInsUpdImg(txtDeleteMenuAdmin.Value, "", "", "", "", "", "", "2");

                GetData();
            }

        }
        private void GetData()
        {
            string email = txtEmail.Text.ToString().Trim();
            DataTable dt = GetListUser(email);
            gvUser.DataSource = dt;
            gvUser.DataBind();
            gvUser.GridLines = GridLines.None;
        }
        private DataTable GetListUser(string email)
        {
            bs_user mybs = new bs_user(ConfigurationManager.ConnectionStrings["ShowRoomConnectionString"].ToString());
            PccMsg myMsg = new PccMsg();
            myMsg.CreateFirstNode("email", email);
            DataSet ds = mybs.DoReturnDataSet("GETLISTUSERBYSEARCH", myMsg.GetXmlStr, "");
            DataTable dt = new DataTable();
            if (ds.Tables.Count > 0)
                dt = ds.Tables[0];
            return dt;
        }

        protected void gvUser_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
                string user_id = drv["user_id"].ToString().Trim();

                ((Label)e.Row.FindControl("lbldateadd")).Text = GetDateTimeStr14(((Label)e.Row.FindControl("lbldateadd")).Text);
                string sex = ((Label)e.Row.FindControl("lblsex")).Text.Trim();
                if (sex == "1")
                    ((Label)e.Row.FindControl("lblsex")).Text = "Nữ";
                else
                    ((Label)e.Row.FindControl("lblsex")).Text = "Nam";

                string active = ((Label)e.Row.FindControl("lblactive")).Text.Trim();
                if (active == "Y")
                    ((Label)e.Row.FindControl("lblactive")).Text = "<img src='img/active.png' />";
                else
                    ((Label)e.Row.FindControl("lblactive")).Text = "<img src='img/inactive.png' />";
                e.Row.Attributes.Add("onmouseover", "setBk(this)");
                e.Row.Attributes.Add("onmouseout", "setBkOut(this)");

                ((Image)e.Row.FindControl("imgEdit")).Style["cursor"] = "pointer";
                ((Image)e.Row.FindControl("imgEdit")).Attributes.Add("onclick", "EditUserAdmin('" + user_id + "')");

                ((Image)e.Row.FindControl("imgDelete")).Style["cursor"] = "pointer";
                ((Image)e.Row.FindControl("imgDelete")).Attributes.Add("onclick", "CheckDeleteUser('" + user_id + "')");
            }
        }
        public static string GetDateTimeStr14(string date)
        {
            string str = date;
            if (date.Length >= 14)
                str = date.Substring(0, 4) + "/" + date.Substring(4, 2) + "/" + date.Substring(6, 2) + " " + date.Substring(8, 2) + ":" + date.Substring(10, 2) + ":" + date.Substring(12, 2);

            return str;
        }
        protected void imgSerach_Click(object sender, ImageClickEventArgs e)
        {
            GetData();
        }
        public string SaveData(string user_id, string user_name, string password, string name, string phone, string email, string sex, string introduction, string adress, string birthday, string activeEmail, string emailPass, string active, string Type)
        {
            bs_user myBs = new bs_user(ConfigurationManager.ConnectionStrings["ShowRoomConnectionString"].ToString());
            PccMsg myMsg = new PccMsg();
            myMsg.CreateFirstNode("user_id", user_id);
            myMsg.CreateFirstNode("name", name);
            myMsg.CreateFirstNode("username", user_name);
            myMsg.CreateFirstNode("password", password);
            myMsg.CreateFirstNode("ipassword", password);
            myMsg.CreateFirstNode("email", email);
            myMsg.CreateFirstNode("adress", adress);
            myMsg.CreateFirstNode("birthday", birthday);
            myMsg.CreateFirstNode("telephone", phone);
            myMsg.CreateFirstNode("sex", sex);
            myMsg.CreateFirstNode("company", "");
            myMsg.CreateFirstNode("point", "10");
            myMsg.CreateFirstNode("note", introduction);
            myMsg.CreateFirstNode("activeEmail", activeEmail);
            myMsg.CreateFirstNode("emailPass", emailPass);
            myMsg.CreateFirstNode("active", active);
            myMsg.CreateFirstNode("Type", Type);
            string strXML = myMsg.GetXmlStr;
            string strReturn = myBs.DoReturnStr("INSUPDUSER", strXML, "");
            myMsg.LoadXml(strReturn);


            return myMsg.Query("Err");
        }
    }
}